/**
 * 
 */
package org.ediba.siri.web.rest;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.ediba.siri.core.model.domain.Model;
import org.ediba.siri.core.model.domain.ModelVisit;
import org.ediba.siri.core.model.service.ModelService;
import org.ediba.siri.core.request.service.RequestService;
import org.json.JSONException;
import org.springframework.web.bind.annotation.ResponseBody;

public class ModelVisitController extends Controller {

	private ModelService modelService;
	public void setModelService(ModelService modelService) {this.modelService = modelService;}
	
	private RequestService requestService;
	public void setRequestService(RequestService requestService) {this.requestService = requestService;}
	
	@ResponseBody
	public Map<String,Object> postHandler(HttpServletRequest request, HttpServletResponse response, ModelVisitCommand command){

		Integer idModel = null;
		Integer idRequest = null;
		Date date = null;
		Boolean delivered = null;
		ModelVisit modelVisit = null;
		try {
			idModel = command.getIdModel();
			idRequest = command.getData().getJSONObject("request").getInt("id");
			date = new Date(command.getData().getLong("date"));
			delivered = command.getData().getBoolean("delivered");
			modelVisit = this.modelService.visitCreate(idModel,idRequest,date,delivered);
		} catch (JSONException e) {e.printStackTrace();}
		
		Map<String,Object> _result = new HashMap<String,Object>(2);
		_result.put("data", modelVisit);
		_result.put("success", modelVisit!=null);
		return _result;
	}
	
	@ResponseBody
	public Map<String,Object> getHandler(HttpServletRequest request, HttpServletResponse response, ModelVisitCommand command){
		List<ModelVisit> modelVisit = null;
		
		Integer idModel = null;
		
		idModel = command.getIdModel();
		
		if (idModel!=null){
			Model model = this.modelService.retrieve(idModel);
			modelVisit = new ArrayList<ModelVisit>(model.getModelvisits());
		}
		
		Map<String,Object> _result = new HashMap<String,Object>(4);
		_result.put("data", modelVisit);
		_result.put("total", modelVisit.size());
		_result.put("success", modelVisit!=null);
		_result.put("message", "OK");
		return _result;
	}
	
	@ResponseBody
	public Map<String,Object> putHandler(HttpServletRequest request, HttpServletResponse response, ModelVisitCommand command){
		Integer id = null;
		Integer idModel = null;
		Integer idRequest = null;
		Date date = null;
		Boolean delivered = null;
		ModelVisit modelVisit = null;
		try {
			id = command.getData().getInt("id");
			idModel = command.getIdModel();
			idRequest = command.getData().getJSONObject("request").getInt("id");
			date = new Date(command.getData().getLong("date"));
			delivered = command.getData().getBoolean("delivered");
			
			modelVisit = this.modelService.visitRetrieve(id);
			modelVisit.setModel(this.modelService.retrieve(idModel));
			modelVisit.setRequest(this.requestService.retrieve(idRequest));
			modelVisit.setDate(date);
			modelVisit.setDelivered(delivered);
			modelVisit = this.modelService.visitUpdate(modelVisit);
		} catch (JSONException e) {e.printStackTrace();}
		
		Map<String,Object> _result = new HashMap<String,Object>(2);
		_result.put("data", modelVisit);
		_result.put("success", modelVisit!=null);
		return _result;
	}
	
	@ResponseBody
	public Map<String,Object> deleteHandler(HttpServletRequest request, HttpServletResponse response, ModelVisitCommand command){

		Integer id = null;
		try {id = command.getData().getInt("id");}
		catch (JSONException e) {e.printStackTrace();}
		
		boolean _success = this.modelService.visitDelete(id);
		
		Map<String,Object> _result = new HashMap<String,Object>(1);
		_result.put("success", _success);
		return _result;
	}
}
